package com.mangowang.module.main.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.mangowang.lib.common.App;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.util.APKVersionCodeUtils;
import com.mangowang.lib.common.util.CacheUtil;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.ruffian.library.widget.RTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import kale.ui.view.dialog.EasyDialog;

public class SettingActivity extends BaseActivity {
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.clear_cache_rlt)
    TextView clearCacheRlt;
    @BindView(R2.id.login_out_tv)
    Button loginOutTv;
    @BindView(R2.id.version_tv)
    TextView versionTv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;

    @BindView(R2.id.login_password_tv)
    RTextView login_password_tv;


    @BindView(R2.id.pay_password_tv)
    RTextView pay_password_tv;

    private boolean isEditSuccess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        tvTitle.setText("设置");
        versionTv.setText(APKVersionCodeUtils.getVerName(mContext));

        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        login_password_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ChangePasswordActivity.class);
                intent.putExtra("title", "修改密码");
                startActivity(intent);
            }
        });

        pay_password_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(mContext, ChangePayPasswordActivity.class);
                intent1.putExtra("captcha_type", "7");
                startActivity(intent1);
            }
        });

        clearCacheRlt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EasyDialog.Builder builder = EasyDialog.builder(mContext); // 建立builder对象
                builder.setTitle("提示")
                        .setMessage("确定清除缓存?")
                        .setOnCancelListener(dialogInterface -> {

                        })
                        .setOnDismissListener(dialogInterface -> {

                        })
                        // 设置下方的三个按钮
                        .setPositiveButton("确定", (dialog, which) -> {
                            CacheUtil.cleanAllCache(mContext);
                            showToast("清除完毕");
                        })
                        .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                        .setCancelable(true); // 点击空白处可以关闭
                DialogFragment easyDialog = builder.build();

                  // 用showAllowingStateLoss()弹出
                easyDialog.show(getSupportFragmentManager(), "");


            }
        });


        loginOutTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                EasyDialog.Builder builder = EasyDialog.builder(mContext); // 建立builder对象
                builder.setTitle("提示")
                        .setMessage("确定退出登录?")
                        .setOnCancelListener(dialogInterface -> {

                        })
                        .setOnDismissListener(dialogInterface -> {
                        })
                        // 设置下方的三个按钮
                        .setPositiveButton("确定", (dialog, which) -> {
                            NetworkManager.INSTANCE.post(Apis.logout, new OnRequestCallBack() {
                                @Override
                                public void onOk(byte[] bytes){
                                }
                                @Override
                                public void onError(int code, String errorMessage) {
                                }
                            });
                            ARouter.getInstance().build("/login/LoginActivity").navigation();
                            UserHelper.loginOut();
                            App.finishAllTempActivity();
                            finish();
                        })
                        .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                        .setCancelable(true); // 点击空白处可以关闭
                DialogFragment easyDialog = builder.build();

                // 用showAllowingStateLoss()弹出
                easyDialog.show(getSupportFragmentManager(), "");


            }
        });


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void close() {
        if (isEditSuccess) setResult(RESULT_OK);
        finish();
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 100) {
            isEditSuccess = true;
        }
    }
}
